package fm.dongman.animefm.view.fragment


import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CenterCrop

import fm.dongman.animefm.R
import fm.dongman.animefm.view.activity.WebViewActivity
import fm.dongman.animefm.view.transform.GlideRoundTransform
import fm.dongman.animefm.view.util.SystemHelper
import kotlinx.android.synthetic.main.fragment_about_us.*
import kotlinx.android.synthetic.main.item_coder.view.*
import org.jetbrains.anko.onClick


/**
 * 关于我们
 * create by alex 2017.11.28
 */
class AboutUsFragment : BaseFragment() {

    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        return inflater!!.inflate(R.layout.fragment_about_us, container, false)
    }

    override fun initData() {
        mTbAboutUs.setPadding(0, SystemHelper.getStatusBarHeight(activity), 0, 0)
        mRvAboutUsCoder.adapter = CoderAdapter(activity)
        mRvAboutUsCoder.layoutManager = GridLayoutManager(activity, 4, GridLayoutManager.VERTICAL, false)
    }

    override fun bindData() {
        mTbAboutUs.onClick {
            activity.finish()
        }

        //官方网站(使用外部浏览器)
        mTvAboutUsWebsite.onClick {
            val intent = Intent(Intent.ACTION_VIEW)
            intent.data = Uri.parse("http://m.dongman.fm")
            activity.startActivity(intent)
        }
        //使用协议(使用本软件内的WebView)
        mTvAboutUsProtocol.onClick {
            activity.startActivity(WebViewActivity.getIntent(activity, "", true))
        }
    }

    private class CoderAdapter(private val mContext: Context)
        : RecyclerView.Adapter<CoderAdapter.ViewHolder>() {

        private val mCoderNames = arrayOf("BevisWang", "kuige", "alert", "追逐流星的Alex")
        private val mCoderAvatars = arrayOf(R.mipmap.shize, R.mipmap.kuige, R.mipmap.beer, R.mipmap.ranking_more)

        override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ViewHolder {
            return ViewHolder(View.inflate(mContext, R.layout.item_coder, null))
        }

        override fun getItemCount(): Int {
            return mCoderNames.size
        }

        override fun onBindViewHolder(holder: ViewHolder?, position: Int) {
            holder!!.itemView.onClick {
            }
            holder.mName.text = mCoderNames[position]
            Glide.with(mContext).load(mCoderAvatars[position])
                    .bitmapTransform(CenterCrop(mContext), GlideRoundTransform(mContext, 36))
                    .into(holder.mCover)
        }

        private class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
            var mName: TextView = itemView.mTvCoderName
            var mCover: ImageView = itemView.mIvCoderAvatar
        }
    }

}
